 ///
 /// @file    test.cc
 /// @author  lemon(haohb13@gmail.com)
 /// @date    2016-03-03 11:39:35
 ///
 

#include "MutexLock.h"
#include "Condition.h"
#include <unistd.h>
#include <pthread.h>
#include <iostream>
using std::cout;
using std::endl;


int iCount = 100;
MutexLock mutexlock;
Condition condition(mutexlock);



void * thread1(void * arg)
{
	while(true)
	{
		mutexlock.lock();
		while(iCount < 90)// 切记：不要使用if进行判断
			condition.wait();

		--iCount;
		cout << "iCount = " << iCount << endl;
		mutexlock.unlock();

		condition.notify();
		sleep(1);
	}
}

void * thread2(void * arg)
{
	while(true)
	{
		mutexlock.lock();
		while(iCount > 90)
			condition.wait();

		++iCount;
		cout << "iCount = " << iCount << endl;
		mutexlock.unlock();
		condition.notify();
		sleep(2);
	}
}

int main(void)
{
	pthread_t pth1;
	pthread_t pth2;

	pthread_create(&pth1, NULL, thread1, NULL);
	pthread_create(&pth2, NULL, thread2, NULL);

	pthread_join(pth1, NULL);
	pthread_join(pth2, NULL);
}
